package com.tiancikeji.zaoke.app.service.user.impl;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.tiancikeji.zaoke.app.dao.ticket.TicketDao;
import com.tiancikeji.zaoke.app.model.user.Ticket;
import com.tiancikeji.zaoke.app.model.user.User;
import com.tiancikeji.zaoke.app.service.user.TicketService;
import com.tiancikeji.zaoke.app.util.ZaokeUtils;

public class TicketServiceImpl implements TicketService{

	private TicketDao ticketDao;
	
	private int ticketLength = 20;

	public Ticket generateTicket(User user) {
		Ticket ticket = null;
		if(user != null){
			ticket = new Ticket();
			ticket.setTicket(genTicket());
			ticket.setUserId(user.getUserId());
			if(StringUtils.isNotBlank(user.getCellPhone()))
				ticket.setCellPhone(user.getCellPhone());
			ticket.setAddUserId(user.getUserId());
			ticket.setAddTime(new Date());
			
			ticketDao.addTicket(ticket);
		}
		return ticket;
	}
	
	private String genTicket() {
		return ZaokeUtils.getRandString(ticketLength);
	}

	public Ticket getUserTicket(long userid, String ticket){
		return ticketDao.getTicketByUser(userid, ticket);
	}

	public TicketDao getTicketDao() {
		return ticketDao;
	}

	public void setTicketDao(TicketDao ticketDao) {
		this.ticketDao = ticketDao;
	}

	public int getTicketLength() {
		return ticketLength;
	}

	public void setTicketLength(int ticketLength) {
		this.ticketLength = ticketLength;
	}

}
